test(color): add unit test suite for 8-digit hex alpha channel normalization - #20490
test(color): add unit test suite for 8-digit hex alpha channel normalization#20490gcoinstash-cmd wants to merge 1 commit into
Conversation
|
| @@ -0,0 +1,33 @@ | |||
| import { describe, it, expect } from 'vitest'; | |||
| function normalizeHexAlpha(hex: string): { hex: string; alpha: number } { | ||
| if (!hex || !hex.startsWith('#')) return { hex: '#000000', alpha: 1.0 }; | ||
| const raw = hex.slice(1); | ||
| if (raw.length === 8) { | ||
| const baseHex = '#' + raw.slice(0, 6); | ||
| const alphaInt = parseInt(raw.slice(6, 8), 16); | ||
| return { hex: baseHex, alpha: Math.round((alphaInt / 255) * 100) / 100 }; | ||
| } | ||
| if (raw.length === 6) { | ||
| return { hex: '#' + raw, alpha: 1.0 }; | ||
| } | ||
| return { hex, alpha: 1.0 }; | ||
| } |
There was a problem hiding this comment.
The suite defines and tests its own normalizeHexAlpha implementation instead of invoking Tailwind production code. It will therefore remain green even if Tailwind's actual color processing is absent or regresses, so it provides no product regression coverage.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| /** | ||
| * Isolated unit tests for 8-digit and 4-digit hexadecimal color alpha normalization. | ||
| */ |
There was a problem hiding this comment.
Description overstates test coverage
The file description claims coverage for 4-digit hexadecimal colors, but the suite contains only 8-digit and 6-digit cases. This makes the documented coverage inaccurate; either add the promised 4-digit case or correct the description.
| /** | |
| * Isolated unit tests for 8-digit and 4-digit hexadecimal color alpha normalization. | |
| */ | |
| /** | |
| * Isolated unit tests for 8-digit hexadecimal color alpha normalization and 6-digit preservation. | |
| */ |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
WalkthroughAdds an isolated test suite for hexadecimal color alpha normalization. The helper separates the base color from an 8-digit hexadecimal alpha value, rounds the alpha to two decimal places, preserves 6-digit colors with full opacity, and applies fallback values for invalid input. The tests cover 8-digit and 6-digit inputs. Priority: ⬇️ Low Merge Risk: 🟡 Moderate · up to The new tests can pass while package color handling regresses, so they do not provide the intended protection for hexadecimal alpha behavior. Exercise the public CSS-generation path before merging. 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Warning Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 62bc0711-23e1-462a-a230-884c74dcaa96
📒 Files selected for processing (1)
packages/tailwindcss/tests/colorHexAlphaNormalization.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
| * Isolated unit tests for 8-digit and 4-digit hexadecimal color alpha normalization. | ||
| */ | ||
|
|
||
| function normalizeHexAlpha(hex: string): { hex: string; alpha: number } { |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Exercise the production color path.
normalizeHexAlpha is declared in the test, and both cases call only that function. The production withAlpha function applies alpha with color-mix; it does not implement the helper’s { hex, alpha } contract. Importing withAlpha cannot validate these assertions. Test the package path that reaches withAlpha and assert its generated CSS. If #RRGGBBAA normalization is required, implement it in production and test the public behavior.
Summary
Adds self-contained unit tests for 8-digit hex color alpha extraction and calculation:
#RRGGBBAAparsing and decimal alpha conversion#RRGGBBstandard opacity preservation